package com.grf.rocketmq.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;


import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @Description: TODO 消息发送
* @Author: grf
* @CreateTime: 2023/9/14 17:10
* @Version: 1.0
*/
@Slf4j
@RestController
@RequestMapping("/send/message")
@RequiredArgsConstructor
public class SendMessageController {

    /**
     * 主题:标签
     */
    public static final String DESTINATION = "test:tag";

    private final RocketMQTemplate rocketMQTemplate;

    /**
     * @Description: TODO 简单同步消息
     * @Author: grf
     * @CreateTime: 2023/9/14 17:11
     * @Param:
     * @Return: SendResult
     **/
    @GetMapping("/sync")
    public SendResult simpleSync(){
        Message<String> msg = MessageBuilder.withPayload("Hello, sync message").build();
        return rocketMQTemplate.syncSend(DESTINATION, msg);
    }

    /**
     * @Description: TODO 直接发送
     * @Param:
     * @Return: void
     **/
    @GetMapping("/oneWay")
    public void oneWay(){
        Message<String> msg = MessageBuilder.withPayload("Hello,oneway message").build();
        rocketMQTemplate.sendOneWay(DESTINATION, msg);
    }

    /**
     * @Description: TODO 异步发送
     * @Param:
     * @Return: void
     **/
    @GetMapping("/async")
    public void async(){
        Message<String> msg = MessageBuilder.withPayload("Hello, async message").build();
        rocketMQTemplate.asyncSend(DESTINATION, msg, new SendCallback(){

            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("发送成功");
            }

            @Override
            public void onException(Throwable throwable) {
                log.error("发送失败");
            }
        });
    }

    /**
     * @Description: TODO 同步顺序消息
     * @Param:
     * @Return: void
     **/
    @GetMapping("/sync/order")
    public void syncOrder(){
        Message<String> msg = MessageBuilder.withPayload("Hello, sync order message").build();
        for(int i=0; i<10; i++){
            rocketMQTemplate.syncSendOrderly(DESTINATION, msg, i<5?"0":"1");
        }
    }

    /**
     * @Description: TODO 异步顺序消息
     * @Param:
     * @Return: void
     **/
    @GetMapping("/async/order")
    public void asyncOrder(){
        Message<String> msg = MessageBuilder.withPayload("Hello, async order message").build();
        for(int i=0; i<10; i++){
            rocketMQTemplate.asyncSendOrderly(DESTINATION, msg, i < 5 ? "0" : "1", new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    log.info("发送成功");
                }

                @Override
                public void onException(Throwable throwable) {
                    log.error("发送失败");
                }
            });
        }
    }

    /**
     * @Description: TODO 同步发送消息并等待String类型的返回值
     * @Param:
     * @Return: SendResult
     **/
    @GetMapping("/sync/and/receive")
    public SendResult syncAndReceive(){
        Message<String> msg = MessageBuilder.withPayload("Hello, sync message and receive value").build();
        return rocketMQTemplate.sendAndReceive(DESTINATION, msg, String.class);
    }

    /**
     * @Description: TODO 延迟消息
     * @Param:
     * @Return: SendResult
     **/
    @GetMapping("/sync/delay")
    public SendResult syncDelay(){
        Message<String> msg = MessageBuilder.withPayload("Hello, sync delay message").build();
        return rocketMQTemplate.syncSend(DESTINATION, msg, 60*1000, 3);
    }
}
